package top.hidoctor.auscultation.feature.activity;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.SeekBar;


import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.target.Target;
import com.bumptech.glide.request.transition.Transition;
import com.google.gson.Gson;
import com.google.gson.internal.LinkedTreeMap;
import com.google.gson.reflect.TypeToken;
import com.tang.base.BaseResponse;
import com.tang.base.NetMapEncryptUtils;
import com.tang.net.NetClient;
import com.tang.util.BigDecimalUtil;
import com.tang.util.DensityUtil;
import com.tang.util.GsonUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import top.hidoctor.auscultation.R;
import top.hidoctor.auscultation.adapter.TestAdapter;
import top.hidoctor.auscultation.base.BaseActivity;
import top.hidoctor.auscultation.bean.ProblemBean;
import top.hidoctor.auscultation.bean.SPBean;
import top.hidoctor.auscultation.net.Api;
import top.hidoctor.auscultation.net.req.DictionaryReq;
import top.hidoctor.auscultation.view.SPlineView;
import top.hidoctor.auscultation.widget.tree.AndroidTreeView;
import top.hidoctor.auscultation.widget.tree.NodeIDFormat;
import top.hidoctor.auscultation.widget.tree.TypeBeanString;

public class TestActivity extends BaseActivity {

    List<TypeBeanString> list = new ArrayList<>();
    private AndroidTreeView mTreeView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_text);
        getDate();

    }

    private void getDate() {
        DictionaryReq req = new DictionaryReq();
        req.setGroupCode("SAMPLE_MARK_OPTION");
        Map<String, Object> maps = new NetMapEncryptUtils(req).getMaps();
        NetClient.getInstance(Api.class)
                .dictionaryList(maps)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new Observer<BaseResponse<Object>>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(BaseResponse<Object> value) {
                        String data = GsonUtil.toJsonStr(((LinkedTreeMap) value.getData()).get("result"));
                        List<ProblemBean> problemBeans = GsonUtil.parseJson(data, new TypeToken<List<ProblemBean>>() {
                        });
                        getList(problemBeans);
                        Log.e("TAG", "onNext: " + GsonUtil.toJsonStr(list));
                        initEvent();
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    private void getList(List<ProblemBean> problemBeans) {
        Log.e("TAG", "getList: " + GsonUtil.toJsonStr(problemBeans));
        for (int i = 0; i < problemBeans.size(); i++) {
            list.add(new TypeBeanString(problemBeans.get(i).getId(), problemBeans.get(i).getParentId(), problemBeans.get(i).getTitle(), problemBeans.get(i).getType() + ""));
            Log.e("TAG", "bean: " + GsonUtil.toJsonStr(problemBeans.get(i)));
            if (problemBeans.get(i).getChildren() != null && problemBeans.get(i).getChildren().size() > 0) {
                getList(problemBeans.get(i).getChildren());
            }
        }
    }

    private void initEvent() {
        RelativeLayout layout = findViewById(R.id.layout_tree_view);
        // 通过构造方法实例化
        mTreeView = new AndroidTreeView(this);
        // 设置开启多选，默认为关闭,可选
        mTreeView.setMultiSelEnable(true);
        // 设置当前bean 中 id pid 的类型为 String，必选，默认为 long
        mTreeView.setNodeIdFormat(NodeIDFormat.STRING);

        mTreeView.setTitleTextColor("#000000");             // 设置显示文本字体颜色，可选
        mTreeView.setTitleTextSize(12);                     // 设置显示文本字体大小，可选
//        mTreeView.setOpenIconRes(R.mipmap.icon_node_open);  // 设置节点打开时显示的图标，可选，
//        mTreeView.setCloseIconRes(R.mipmap.icon_node_close);// 设置节点关闭时显示的图标，可选
        mTreeView.setLevelPadding(20);                       // 设置层级缩进大小，可选
//        mTreeView.setIntervalColor("#aabbaa");              // 设置节点间隔线颜色，可选
//        mTreeView.setIntervalHeight(0.8f);                   // 设置节点间隔线高度，可选

        // 绑定数据，必须在最后一步调用，否则上述自定义数据将无效
        mTreeView.bindData(list);

        // 将 TreeView 添加到 FrameLayout 中显示
        layout.addView(mTreeView);

    }


}
